package vn.pyco.prez.tapestry.tasks;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import vn.pyco.prez.dao.DaoManager;
import vn.pyco.prez.model.Slide;

public abstract class ConvertionTask implements Runnable, ProgressNotifier {
	private static final Logger logger = LoggerFactory.getLogger(ConvertionTask.class); 
	
	private double _progress = 0;
	private String _id;
	private File _fileToConvert;
	private DaoManager<List<Slide>> _slideManager;
	private ProgressHandler _handler;
	private List<Slide> _slides;
		
	public ConvertionTask(File fileToConvert, DaoManager<List<Slide>> slideManager) {
		_id = UUID.randomUUID().toString();
		_fileToConvert = fileToConvert;
		_slideManager = slideManager;
		_slides = new ArrayList<Slide>();
	}
	
	/**
	 * @return the id
	 */
	public String getId() {
		return _id;
	}

	public File getFileToConvert() {
		return _fileToConvert;
	}

	public DaoManager<List<Slide>> getSlideManager() {
		return _slideManager;
	}

	public List<Slide> getSlides(){
		return _slides;
	}
	
	public void setProgress(double percent) {
		
		_progress = percent;
		notifyProgress(_progress);
	}

	public double getProgress() {
		return _progress;
	}
	
	@Override
	public void setProgressHandler(ProgressHandler handler) {
		_handler = handler;
	}

	@Override
	public void notifyProgress(double percent) {
		if(_handler != null) {
			logger.debug("Notify Progress is at " + percent + "% for task : "+ _id);
			_handler.notifyProgress(_id,percent);
		} else {
			logger.warn("ProgressNotifier has not been set for this ConvertionTask");
		}
		
	}
}